import {getTemplateInfo} from '@/api/home.js';

const state = {
	menuButtonInfo: {},
	globalStyle: {},
	headerStyle: {},
	dialogConfig: {},
	searchModule: {},
	indexModuleList: []
}

const actions = {
	async getTemplateInfo({state, commit}) {
		await getTemplateInfo({type: 0,appType: 0}).then(res => {
			if (res.success) {
				const jsonTmp = JSON.parse(res.data.content);
				const globalConfig = jsonTmp.globalConfig;
				const dialogConfig = jsonTmp.dialogConfig || {};
				commit('setDialogConfog', dialogConfig);
				commit('setGlobalStyle', globalConfig);
				const headerStyle = {};
				if (globalConfig.headerBgImg) {
					headerStyle.background = `url(${globalConfig.headerBgImg}) no-repeat`;
					headerStyle.backgroundSize = '100% auto';
				} else {
					headerStyle.backgroundColor = globalConfig.headerBackground;
				}
				headerStyle.paddingTop = state.menuButtonInfo.top -6 + 'px';
				headerStyle.height = state.menuButtonInfo.top + state.menuButtonInfo.height + 6 + 'px';
				commit('setHeaderStyle', headerStyle);
				const moduleListTmp = jsonTmp.moduleList || [];
				const searchModuleTmp = moduleListTmp.find(item => item.type === 1);
				if (searchModuleTmp) {
					commit('setSearchModule', searchModuleTmp);
				}
				commit('setIndexModuleList', moduleListTmp.filter(item => item.type !== 1));
				uni.$emit('refreshTmpData');
			}
		}).catch(error => {
			console.log(error)
		})
	}
}

const mutations = {
	setMenuButtonInfo (state, data) {
		state.menuButtonInfo = data;
	},
	setGlobalStyle(state, data) {
		state.globalStyle = data;
	},
	setHeaderStyle(state, data) {
		state.headerStyle = data;
	},
	setSearchModule(state, data) {
		state.searchModule = data;
	},
	setIndexModuleList(state, data) {
		state.indexModuleList = data;
	},
	setDialogConfog(state, data) {
		state.dialogConfig = data;
	}
}  
export default {
	namespaced: true,
	state, 
	actions,
	mutations
}